﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyDetailControl : RSBaseUserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["selindex"] != null)
                {
                    int selectedindex = Convert.ToInt32(Request.QueryString["selindex"].ToString());
                    Accordion1.SelectedIndex = selectedindex;
                }
            }
        }

        protected void btnSavePropertyAddress_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE dbo.rs_tblProperty SET " +
                    "SubdivisionName = @SubdivisionName, " +
                    "Street = @Street, City = @City, State = @State, Zip = @Zip, County = @County, Country = @Country, " +
                    "Audit_UpdateBy = @CurrentUser, Audit_UpdateDt = GETDATE() " +
                    "WHERE CompanyID=@CompanyId AND PropertyID=@PropertyID";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            int PropertyID = hfPropertyID.Value == "" ? 0 : Convert.ToInt32(hfPropertyID.Value);
            string SubdivisionName = txtSubdivisionName.Text;
            string Street = txtStreet.Text;
            string City = txtCity.Text;
            string State = txtState.Text;
            string County = txtCounty.Text;
            string Country = txtCountry.Text;
            string Zip = txtZip.Text;

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@SubdivisionName", SqlDbType.NVarChar, 150, ParameterDirection.Input, SubdivisionName);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 250, ParameterDirection.Input, Street);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 50, ParameterDirection.Input, City);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 50, ParameterDirection.Input, State);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 10, ParameterDirection.Input, Zip);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@County", SqlDbType.NVarChar, 50, ParameterDirection.Input, County);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Country", SqlDbType.NVarChar, 50, ParameterDirection.Input, Country);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Property Address Successfully Saved!", RedirectToCurrentURL("0"), true);
        }
        protected void btnSavePropertyInfo_Click(object sender, EventArgs e)
        {
            string SQL = "UPDATE dbo.rs_tblProperty SET " +
                    "PropertyTypeID = @PropertyTypeID, " +
                    "YearBuilt = @YearBuilt, SqFt = @SqFt, BedNum = @BedNum, BathNum = @BathNum, AppraisedValue = @AppraisedValue, AssessedValue = @AssessedValue, " +
                    "ElementarySchool = @ElementarySchool, MiddleSchool = @MiddleSchool, HighSchool = @HighSchool, " +
                    "Audit_UpdateBy = @CurrentUser, " +
                    "Audit_UpdateDt = GETDATE() " +
                    "WHERE CompanyID=@CompanyId AND PropertyID=@PropertyID";

            RSUserIdentity userIdentity = GetRSUserIdentity();
            int PropertyID = hfPropertyID.Value == "" ? 0 : Convert.ToInt32(hfPropertyID.Value);
            string Description = txtDescription.Text;
            int PropertyTypeID = int.Parse(ddlPropertyType.SelectedValue);
            int YearBuilt = txtYearBuilt.Text == "" ? 0 : int.Parse(txtYearBuilt.Text);
            int SqFt = txtSqFt.Text == "" ? 0 : int.Parse(txtSqFt.Text);
            float BedNum = txtBedNum.Text == "" ? 0 : float.Parse(txtBedNum.Text);
            float BathNum = txtBathNum.Text == "" ? 0 : float.Parse(txtBathNum.Text);
            string AppraisedValue = txtAppraisedValue.Text;
            string AssessedValue = txtAssessedValue.Text;
            string ElementarySchool = txtElementarySchool.Text;
            string MiddleSchool = txtMiddleSchool.Text;
            string HighSchool = txtHighSchool.Text;

            SqlCommand sqlCmd = new SqlCommand();
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@YearBuilt", SqlDbType.Int, 0, ParameterDirection.Input, YearBuilt);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@SqFt", SqlDbType.Int, 0, ParameterDirection.Input, SqFt);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BedNum", SqlDbType.Float, 0, ParameterDirection.Input, BedNum);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@BathNum", SqlDbType.Float, 0, ParameterDirection.Input, BathNum);
            
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AppraisedValue", SqlDbType.NVarChar, 250, ParameterDirection.Input, AppraisedValue);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@AssessedValue", SqlDbType.NVarChar, 250, ParameterDirection.Input, AssessedValue);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@ElementarySchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, ElementarySchool);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@MiddleSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, MiddleSchool);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@HighSchool", SqlDbType.NVarChar, 250, ParameterDirection.Input, HighSchool);

            SQLHelper.AddParamToSQLCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 250, ParameterDirection.Input, Description);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyTypeID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyTypeID);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, PropertyID);
            SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
            SQLHelper.SetCommandType(sqlCmd, CommandType.Text, SQL);
            SQLHelper.ExecuteScalarCmd(sqlCmd);

            int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
            ucModalActionMessageControl.ShowModal("Property Information Successfully Saved!", RedirectToCurrentURL("1"), true);
        }
        
        public bool LoadPropertyData(int companyid, int propertyid)
        {
            bool bHasValue = false;
            RSPRMDataLookupLoader.LoadTypeDDL(companyid, "PROPERTY_CAT", ddlPropertyType);

            if (companyid > 0 && propertyid > 0)
            {
                hfPropertyID.Value = propertyid.ToString();

                String strSQL = "EXEC rs_uspGetPropertyByID @CompanyID, @PropertyID";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(strSQL, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@CompanyID";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@PropertyID";
                param.Value = propertyid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    //Property Address
                    txtSubdivisionName.Text = myDataReader["SubdivisionName"].ToString();
                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtCounty.Text = myDataReader["County"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtCountry.Text = myDataReader["Country"].ToString();

                    //Property Info
                    txtBedNum.Text = myDataReader["BedNum"].ToString();
                    txtBathNum.Text = myDataReader["BathNum"].ToString();
                    txtYearBuilt.Text = myDataReader["YearBuilt"].ToString();
                    txtAppraisedValue.Text = myDataReader["AppraisedValue"].ToString();
                    txtAssessedValue.Text = myDataReader["AssessedValue"].ToString();
                    txtElementarySchool.Text = myDataReader["ElementarySchool"].ToString();
                    txtMiddleSchool.Text = myDataReader["MiddleSchool"].ToString();
                    txtHighSchool.Text = myDataReader["HighSchool"].ToString();
                    txtDescription.Text = myDataReader["Description"].ToString();

                    if (myDataReader["PropertyTypeID"] != DBNull.Value)
                    {
                        ddlPropertyType.SelectedValue = myDataReader["PropertyTypeID"].ToString();
                    }

                    ucPropertyFeatureControl.LoadPropertyFeatures(companyid, propertyid);
                    ucOwnerControl.LoadOwnerData(companyid, propertyid);
                    bHasValue = true;
                }
                myConnection.Close();

            }
            return bHasValue;
        }
        private string RedirectToCurrentURL(string activetab)
        {
            var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            nameValues.Set("selindex", activetab);
            string url = Request.Url.AbsolutePath;
            string updatedQueryString = "?" + nameValues.ToString();
            url = url + updatedQueryString;
            return url;
        }
    }
}